<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : systemframework.class.php   FILE_PATH : \\classes\module\systemframework.class.php
 * ....系统模块调用类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Sat Sep 08 10:29:07 CST 2007
 */

class SystemFrameWork extends FrameWork{
	/**
	 * 系统菜单数组
	 *
	 * @var array
	 */
	var $sysmenu=array();
	/**
	 * 系统管理操作对象
	 *
	 * @var obj
	 */
	var $obj_system_power;
	/**
	 * 系统后台构造函数
	 *
	 * @return bool
	 */
	function SystemFrameWork(){
		//初始化父类框架
		parent::setFrameWork();
		//设置系统后台模板路径
		$this->setsubtemplates($this->_configinfo['websit']['systemadmin']);
		//取多语言类别
		$this->_getLangType();
		//设置smarty缓存
		$this->_objsmarty->caching = false;

		if($this->error == '1'){
			return false;
		}

		//日志操作类
		require_once("system_power.class.php");
		//判断管理员是否登陆
		$this->checkSysMember();
		//公共模块，系统后台菜单目录
		$this->publicModule();
		//检查登录管理员权限
		$this->memberAccp();
		//对参数进行unescape解码
		$this->_input = $this->_param_unescape($this->_input,$this->_configinfo['websit']['ncharset']);
		return true;
	}
	/**
	 * 对参数进行unescape解码
	 *
	 * @param array $param 需要转义的内容
	 * @param string $charset 系统当前编码类型
	 * @return array $param 返回数组形式的转义结果
	 */
	function _param_unescape($param,$charset){
		if (is_array($param)){
			foreach ($param as $k => $v){
				if (is_array($v)){
					$param[$k] = $this->_param_unescape($v,$charset);
				}else {
					$param[$k] = Common::unescape($v,$charset);
				}
			}
			return $param;
		}else {
			return $param;
		}
	}
	
	/**
	 * 取得权限菜单
	 *
	 * @return array $navers 返回数组形式的菜单
	 */
	function navmenu(){
		if (!file_exists(BasePath.'/config/system_menu.php')){
			$this->redirectPath('error','',$this->_lang['langSysCMenuFileIsNotExists']);
		}
		include('system_menu.php');
		return $navers;
	}
	
	/**
	 * 抛出取得权限菜单内容
	 *
	 * @return string 返回输出页面的内容
	 */
	function publicModule(){
		//菜单目录
		$navers = $this->navmenu();
		/**
		 * 判断管理员是否登录
		 */
		if ($_SESSION['s_admin'] != ""){
			/**
			 * 对管理员权限进行判断
			 */
			$array_one = array();//大目录
			foreach ($navers as $key => $value){
				if (is_array($_SESSION['s_admin']['power'])){
					foreach ($_SESSION['s_admin']['power'] as $value2){
						if ($value['power'] == $value2){
							$array_one[] = $value;
						}
					}
				}
			}
			$url = $_SERVER['REQUEST_URI'];
			foreach ($array_one as $key => $value){
				if (!empty($value['body'])){
					$array_two = array();
					foreach ($_SESSION['s_admin']['power'] as $value2){
						$explode_array = array();
						foreach ($value['body'] as $value3){
							if ($value2 == $value3['power']){
								$array_two[] = $value3;
							}
						}
					}
					$array_one[$key]['body'] = "";
					$array_one[$key]['body'] = $array_two;
				}
			}
			/**
			 * 后台调用系统菜单
			 */
			$this->sysmenu = $array_one;
			/**
			 * 输出到模板
			 */
			$this->output('navers',$array_one);
		}
		$this->output('navers_all',$navers);
	}
	/**
	 * 会员权限检查
	 *
	 * @return bool true
	 */
	function memberAccp(){
		if (!empty($_SESSION['s_admin']['power'])){
			$currentFile = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1,-4);
			require_once(BasePath."/system/file_accepttable.php");
			$acceptNum = FileAccept::File_Accept($currentFile);
			if (intval($acceptNum)>0){
				$user_accep_string = preg_replace("/\D+/","|",@implode("|",$_SESSION['s_admin']['power']));
				$user_accep_array = explode("|",$user_accep_string);
				if (!in_array($acceptNum,$user_accep_array)){
					$this->redirectPath('error','',"Sorry! You don't have permission to operate!  <a href='javascript:window.history.back(-1);'>back</a>",1);
				}
			}
			return true;
		}
	}

	/**
	 * 判断管理员是否登陆
	 *
	 * @return string 返回页面内容
	 */
	function checkSysMember(){
		/**
		 * 语言包
		 */
		$this->getlang("sys_index");
		if ($_SESSION['s_admin'] == '' && $this->_input['state'] != 1){//未登录
//			$this->output('url',$this->_configinfo['websit']['site_url']);
//			$this->showpage('sys_index.login');
			echo '<script>top.location.href="'.$this->_configinfo['websit']['site_url'].'/system/login.php";</script>';
			exit;
		}else {
			//判断如果操作不是登录操作的话，则读库验证密码是否正确
			if ($this->_input['action'] !== 'check_login'){
				if (!is_object($this->obj_system_power)){
					$this->obj_system_power = new SystemPowerClass();
				}
				$member_array = $this->obj_system_power->getSysMemberById($_SESSION['s_admin']['id']);

				if (empty($member_array) || ($member_array['sys_password'] !== $_SESSION['s_admin']['password'])){
					unset($_SESSION['s_admin']);
//					$this->output('url',$this->_configinfo['websit']['site_url']);
//					$this->showpage('sys_index.login');
					echo '<script>top.location.href="'.$this->_configinfo['websit']['site_url'].'/system/login.php";</script>';
					exit;
				}
			}
		}
	}

	/**
	 * 重写设置模板子目录方法，用于系统模板路径
	 *
	 * @param string $templatesname 模版子路径
	 * @return string 返回输出内容
	 */
	function setsubtemplates($templatesname){
		$this->_subtemplates = $templatesname;  //模版子路径

		if($this->_subtemplates != ""){
			$this->_subtemplates = "/" . $this->_subtemplates;
		}
		$this->_objsmarty->template_dir   = Templates . "/" . $this->_subtemplates;                    //设置模板目录
		$this->_objsmarty->compile_dir    = Templates . "/templates_c";	//设置编译目录
		$this->_objsmarty->assign("templates_subpath",      "/templates". $this->_subtemplates);     //抛出摸班子路径
		$this->_objsmarty->assign("template_dir",  $this->_configinfo['websit']['site_url'] . "/templates"); //摸板路径
	}
	
	/**
	 * 取语言包类别
	 *
	 * @return string 语言包文件
	 */
	function _getLangType(){
		$this->getlang("common");
		$this->getlang("sys_common");
	}
}
?>